<?php
class Business extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'form_validation' );
		$this->load->model ( 'Muser' );
	}
	
	function index() {
		redirect ( base_url () . 'business/businessList' );
	}
	
	function businessList() {
		$tmp = $this->uri->segment ( 3, 0 );
		$this->load->helper ( 'form' );
		
		$this->load->model ( 'Mabout' );
		$data ['uid'] = $this->session->userdata ( 'uid' );
		$data ['name'] = $this->session->userdata ( 'uname' );
		$data ['cardAboutList'] = $this->Mabout->getAboutListbyClass ( 'C' );
		$data ['aboutList'] = $this->Mabout->getAboutListbyClass ( 'A' );
		
		if (strrpos ( $tmp, '_' )) {
			$data ['class1'] = substr ( $tmp, 0, strrpos ( $tmp, '_' ) );
			$data ['search'] = urldecode ( substr ( $tmp, strrpos ( $tmp, '_' ) + 1 ) );
		} else {
			$data ['search'] = '';
			$data ['class1'] = $tmp;
		}
		$data ['class2'] = $this->uri->segment ( 4, 0 );
		$data ['region'] = $this->uri->segment ( 5, 0 );
		$data ['area'] = $this->uri->segment ( 6, 0 );
		$data ['err'] = '';
		$data ['tClass1'] = '';
		$data ['tClass2'] = '';
		$data ['tRegion'] = '';
		$data ['tArea'] = '';
		$data ['url'] = '';
		$where=NULL;
		
		if ($this->input->post ( 'search' )) {
			$tmp = preg_replace ( "/[^ " . chr ( 0x80 ) . "-" . chr ( 0xff ) . "\\w]+/ ", ' ', $this->input->post ( 'search' ) );
			$tmp = urlencode ( $tmp );
			$tmp = strtr ( $tmp, array ('+' => "%20" ) );
			redirect ( base_url () . 'business/businessList/0_' . $tmp );
		}
		
		$this->load->model ( 'Mbusiness' );
		$this->load->library ( 'pagination' );
		
		if ($data ['class2'] > 0) {
			$tmp = $this->Mbusiness->getClass2byId ( $data ['class2'] );
			if ($tmp) {
				if ($tmp ['class1'] == $data ['class1']) {
					$where ['class2'] = $data ['class2'];
					$data ['tClass2'] = $tmp ['name'];
				} else {
					$where ['class2'] = $data ['class2'];
					$data ['tClass2'] = $tmp ['name'];
					$data ['class1'] = $tmp ['class1'];
					$data ['err'] = '1级分类错误';
				}
			} else {
				$data ['err'] = '找不到该分类';
				$data ['class2'] = 0;
			}
		}
		if ($data ['class1'] > 0) {
			$tmp = $this->Mbusiness->getClass1byId ( $data ['class1'] );
			if ($tmp) {
				$where ['class1'] = $data ['class1'];
				$data ['tClass1'] = $tmp ['name'];
			} else
				$data ['err'] = '找不到该分类';
		}
		
		if ($data ['area'] > 0) {
			$tmp = $this->Mbusiness->getAreabyId ( $data ['area'] );
			if ($tmp) {
				if ($tmp ['region'] == $data ['region']) {
					$where ['area'] = $data ['area'];
					$data ['tArea'] = $tmp ['name'];
				} else {
					$where ['area'] = $data ['area'];
					$data ['tArea'] = $tmp ['name'];
					$data ['region'] = $tmp ['region'];
					$data ['err'] = '行政区错误';
				}
			} else {
				$data ['err'] = '找不到该商区';
				$data ['area'] = 0;
			}
		}
		
		if ($data ['region'] > 0) {
			$tmp = $this->Mbusiness->getRegionbyId ( $data ['region'] );
			if ($tmp) {
				$where ['region'] = $data ['region'];
				$data ['tRegion'] = $tmp ['name'];
			} else
				$data ['err'] = '找不到该行政区';
		}
		
		if ($data ['search'] != '') {
			$searchRow = explode ( ' ', $data ['search'] );
			$search = '(';
			foreach ( $searchRow as $row ) {
				if ($row) {
					$search .= "business.name like '%" . $row . "%' OR ";
					$search .= "business.groupName like '%" . $row . "%' OR ";
					$search .= "business.address like '%" . $row . "%' OR ";
					$search .= "business.phone like '%" . $row . "%' OR ";
					$search .= "business.details like '%" . $row . "%' OR ";
				}
			}
			$search = substr ( $search, 0, strlen ( $search ) - 3 );
			if ($search) $search .= ')';
			$tmp = urlencode ( $data ['search'] );
			$tmp = strtr ( $tmp, array ('+' => "%20" ) );
			$data ['url'] = '_' . $tmp;
		} else
			$search = '';
		
		$data ['group'] = urldecode($this->uri->segment ( 7, 0 ));
		$data ['page'] = $this->uri->segment ( 8, 0 );
		$data ['num'] = $this->Mbusiness->getBusinessNum ( $where, $search, $data['group'] );
		
		$config ['base_url'] = base_url () . 'business/businessList/' . $data ['class1'] .$data ['url']. '/' . $data ['class2'] . '/' . $data ['region'] . '/' . $data ['area'] . '/'.$this->uri->segment ( 7, 0 ).'/';
		$config ['total_rows'] = $data ['num'];
		$config ['per_page'] = PAGE_SHOW_NUM;
		$config ['uri_segment'] = 8;
		$config ['num_links'] = 2;
		$this->pagination->initialize ( $config );
		
		$data ['businessList'] = $this->Mbusiness->getBusinessListbyWhere ( $where, $data ['page'], PAGE_SHOW_NUM, $search, $data['group'] );
		if ($data ['businessList']) {
			$data ['class1List'] = $this->Mbusiness->getClass1List ( $data ['class1'], $search, $data ['region'], $data ['area'],$data ['group'] );
			$data ['class2List'] = $this->Mbusiness->getClass2List ( $data ['class1'], $data ['class2'], $search, $data ['region'], $data ['area'],$data ['group'] );
			$data ['regionList'] = $this->Mbusiness->getRegionList ( $data ['region'], $search, $data ['class1'], $data ['class2'],$data ['group'] );
			$data ['areaList'] = $this->Mbusiness->getAreaList ( $data ['region'], $data ['area'], $search, $data ['class1'], $data ['class2'],$data ['group'] );
			
			$data ['form'] = array ('id' => 'searchForm' );
			$this->load->view ( 'business_businessList', $data );
		} else {
			$data ['msg'] = 'businessList_none';
			$this->load->view ( 'message', $data );
		}
	}
	
	function uploading() {
		$config ['upload_path'] = './uploads/business/';
		$config ['allowed_types'] = 'xls|xlsx';
		$config ['overwrite'] = true;
		$this->load->library ( 'upload', $config );
		
		if (! $this->upload->do_upload ( 'userfile' )) {
			$error = array ('msg' => $this->upload->display_errors () );
		} else {
			$data = $this->upload->data ();
			$filePath = './uploads/business/' . $data ['file_name'];
			
			require_once 'application/libraries/PHPExcel.php';
			require_once 'application/libraries/PHPExcel\IOFactory.php';
			require_once 'application/libraries/PHPExcel\Reader\Excel5.php';
			
			$PHPExcel = new PHPExcel ();
			$PHPReader = new PHPExcel_Reader_Excel2007 ();
			if (! $PHPReader->canRead ( $filePath )) {
				$PHPReader = new PHPExcel_Reader_Excel5 ();
				if (! $PHPReader->canRead ( $filePath )) {
					$error ['msg'] = '不能读取文件' . $filePath;
					$this->load->view ( 'business_uploading', $error );
					return;
				}
			}
			$PHPExcel = $PHPReader->load ( $filePath );
			$currentSheet = $PHPExcel->getSheet ( 0 );
			$allColumn = $currentSheet->getHighestColumn ();
			$allRow = $currentSheet->getHighestRow ();
			if ($PHPExcel->getActiveSheet ()->getCell ( "A1" )->getValue () == "特约商户记录表") {
				for($j = 3; $j <= $allRow; $j ++) {
					$str = '';
					for($k = 'A'; $k <= $allColumn; $k ++) {
						$str .= $PHPExcel->getActiveSheet ()->getCell ( "$k$j" )->getValue () . '\\';
					}
					$strs = explode ( "\\", $str );
					if ($strs [0] == NULL)
						break;
					$mdata ['name'] = $strs [0];
					$mdata ['address'] = $strs [1];
					$mdata ['phone'] = $strs [2];
					$mdata ['class1'] = $strs [3];
					$mdata ['class2'] = $strs [4];
					$mdata ['region'] = $strs [5];
					$mdata ['area'] = $strs [6];
					$mdata ['recycle'] = $strs [7];
					$mdata ['details'] = $strs [8];
					$mdata ['logoImage'] = './uploads/image/business0.jpg';
					$mdata ['show'] = 'F';
					$mdata ['creatTime'] = date ( "Y/m/d H:i:s", time () );
					$mdata ['updateTime'] = date ( "Y/m/d H:i:s", time () );
					$this->load->model ( 'Mbusiness' );
					$bid = $this->Mbusiness->addBusiness ( $mdata );
					if (! $bid) {
						$error ['msg'] = '数据库写入失败！';
						$this->load->view ( 'business_uploading', $error );
						return;
					}
				}
				$error ['msg'] = '上传成功！';
			} else
				$error ['msg'] = 'excel 模板错误，请检查是否选择了正确的文件。';
		}
		
		$this->load->view ( 'business_uploading', $error );
	}
}